import uvicorn
from fastapi import FastAPI
from fastapi import APIRouter

# 1、APIRouter 路由注册方式
app = FastAPI(routes=None)
router_user = APIRouter(prefix="/user", tags=["用户模块"])
router_pay = APIRouter(prefix="/pay", tags=["支付模块"])


@router_user.get("/login")
def user_login():
    return {"ok": "登录成功"}


@router_pay.get("/order")
def pay_order():
    return {"ok": "订单支付成功"}


# 添加路由分组
app.include_router(router_user)
app.include_router(router_pay)

# 2.给路由配置多个HTTP请求方法
router_user = APIRouter(prefix="/user", tags=["用户模块"])


@router_user.get("/login")
def user_login():
    return {"ok": "user_login"}


@router_user.api_route('/user/api/login', methods=['GET', 'POST'])
def user_api_route_login():
    return {"ok": "user_api_route_login"}


def add_user_api_route_login():
    return {"ok": "add_user_api_route_login"}


router_user.add_api_route("/user/add/api/login", methods=["GET", "POST"], endpoint=add_user_api_route_login)

app.include_router(router_user)

if __name__ == '__main__':
    import uvicorn
    import os

    app_model_name = os.path.basename(__file__).replace(".py", "")
    print(app_model_name)
    uvicorn.run(f"{app_model_name}:app", reload=True)
